package com.sgxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sgxy.entity.UserInf;
import com.sgxy.service.UserInfService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Controller
public class UserInfController {
    private final UserInfService userInfService;
    @Autowired
    public UserInfController(UserInfService userInfService) {
        this.userInfService = userInfService;
    }

    /**
     * 处理密码的更新
     * @return
     * 修改成功返回登录主界面
     * 否则留在（跳转）原页面
     */
    @RequestMapping("/editPassword")
    public ModelAndView editPasswordAction(Integer regUUID,String newPassword, String oldPassword){
        ModelAndView modelAndView = new ModelAndView();
        UserInf userInf = userInfService.queryUserInf(regUUID);
        if(userInf==null) {
            modelAndView.addObject("info","修改密码错误！");
            modelAndView.addObject("regUUID",regUUID);
            modelAndView.setViewName("editPassword/editPassword.jsp");
            return modelAndView;
        }
        String password = userInf.getPassword();
        if(oldPassword!=null && oldPassword.equals(password)){
            userInf.setPassword(newPassword);
            userInfService.updateUserInf(userInf);
            modelAndView.addObject("userInf",userInf);
            modelAndView.addObject("regUUID",regUUID);
            modelAndView.addObject("info","修改成功！");
            modelAndView.addObject("flag","TRUE");
        }else{
            modelAndView.addObject("info","当前密码不正确！");
            modelAndView.addObject("regUUID",regUUID);
            modelAndView.addObject("flag","FALSE");
        }
        modelAndView.setViewName("editPassword/editPassword.jsp");
        return modelAndView;
    }

    /**
     * 跳转到修改密码界面
     * @param regUUID
     * 当前用户的id
     * @return
     * 跳转到修改密码界面
     */
    @RequestMapping("/toEditPassword")
    public ModelAndView toEditPassword(String regUUID){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("regUUID",regUUID);
        modelAndView.setViewName("editPassword/editPassword.jsp");
        return modelAndView;
    }

    /**
     *
     * 显示所有用户信息
     * @param page
     * 分页
     */
    @RequestMapping("/userInf")
    public ModelAndView queryAllUserInfAction(Integer page, String username, Integer status){
        ModelAndView modelAndView = new ModelAndView();
        if(page==null){
            page = 1;
        }

        PageHelper.startPage(page,10);
        Example example = new Example(UserInf.class);
        Example.Criteria criteria = example.createCriteria();
        if(username!=null && username!=""){
//            criteria.andEqualTo("username",username);
            criteria.andLike("username","%"+username+"%");

        }
        if(status!=null){
            criteria.andEqualTo("status",status);
        }
        List<UserInf> userInfs = userInfService.queryAllUserInfsByConditon(example);
//        List<UserInf> userInfs = userInfService.queryAllUserInfs();
        PageInfo<UserInf> pageInfo = new PageInfo<>(userInfs);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.addObject("username",username);
        modelAndView.addObject("status",status);
        modelAndView.setViewName("/user/user.jsp");
        return modelAndView;
    }

    /**
     * 跳转到更新页面
     * @param id
     * 指定用户信息的id
     */
    @RequestMapping("/toUpdateUser")
    public ModelAndView toUpdateAction(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("id",id);
        UserInf userInf = userInfService.queryUserInf(id);
        modelAndView.addObject("userInf",userInf);
        modelAndView.setViewName("user/showUpdateUser.jsp");
        return modelAndView;
    }

    /**
     * 更新用户数据
     * @param userInf
     * 存储更新数据的实体
     */
    @RequestMapping("/updateUser")
    public ModelAndView updateUserAction(UserInf userInf){
        ModelAndView modelAndView = new ModelAndView();
        int i = userInfService.updateUserInf(userInf);
        if(i>0){
            modelAndView.addObject("info","TRUE");
        }else{
            modelAndView.addObject("info","FALSE");
        }
        modelAndView.setViewName("redirect:/userInf");
        return modelAndView;
    }

    @RequestMapping("/toAddUser")
    public ModelAndView toAddAction(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user/showAddUser.jsp");
        return modelAndView;
    }

    @RequestMapping("/addUser")
    public ModelAndView addUserAction(UserInf userInf){
        ModelAndView modelAndView = new ModelAndView();
        int i = userInfService.addUserInf(userInf);
        if(i>0){
            modelAndView.addObject("info","TRUE");
        }else{
            modelAndView.addObject("info","FALSE");
        }
        modelAndView.setViewName("redirect:/toAddUser");
        return modelAndView;
    }

    @RequestMapping("/deleteUser")
    public ModelAndView deleteUserAction(String ids){
        ModelAndView modelAndView = new ModelAndView();
        String[] split = ids.split(",");
        Integer[] ids_i = new Integer[split.length];
        for (int i = 0; i < split.length; i++) {
            ids_i[i] = Integer.valueOf(split[i]);
        }
        userInfService.deleteUserInf(ids_i);
//        if(i>0){
//            modelAndView.addObject("info","TRUE");
//        }else{
//            modelAndView.addObject("info","FALSE");
//        }
        modelAndView.setViewName("redirect:/userInf");
        return modelAndView;
    }
}
